package cn.edu.imau.zy.activiti.controller;

import cn.edu.imau.zy.activiti.service.IBizBusinessService;
import cn.edu.imau.zy.common.core.controller.BaseController;
import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.entity.activiti.BizBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程业务 提供者
 * 
 *
 * @date 2020-01-06
 */
@RestController
@RequestMapping("business")
public class BizBusinessController extends BaseController
{
    @Autowired
    private IBizBusinessService bizBusinessService;

    /**
     * 启动业务流程
     */
    @PostMapping("start")
    public R startProcess(@RequestBody BizBusiness business){
        bizBusinessService.insertBizBusiness(business);
        bizBusinessService.startProcess(business, business.getVariables());
        return R.ok();
    }

    /**
     * 查询流程业务
     */
    @GetMapping("get/{id}")
    public BizBusiness get(@PathVariable("id") String id)
    {
        return bizBusinessService.selectBizBusinessById(id);
    }

    /**
     * 查询流程业务
     */
    @GetMapping("getMap/{ids}")
    public Map<String,BizBusiness> getMap(@PathVariable("ids") String ids)
    {
        List<BizBusiness> list = bizBusinessService.selectBizBusinessByTableIds(ids);
        Map<String,BizBusiness> map = new HashMap<>();
        for (BizBusiness business : list) {
            map.put(business.getTableId(),business);
        }
        return map;
    }

    /**
     * 查询流程业务列表
     */
    @GetMapping("list/my")
    public R list(BizBusiness bizBusiness)
    {
        startPage();
        bizBusiness.setUserId(getCurrentUserId());
        bizBusiness.setDelFlag(false);
        return result(bizBusinessService.selectBizBusinessList(bizBusiness));
    }

    /**
     * 新增保存流程业务
     */
    @PostMapping("save")
    public R addSave(@RequestBody BizBusiness bizBusiness)
    {
        bizBusiness.setUserId(getCurrentUserId());
        return toAjax(bizBusinessService.insertBizBusiness(bizBusiness));
    }

    /**
     * 修改保存流程业务
     */
    @PostMapping("update")
    public R editSave(@RequestBody BizBusiness bizBusiness)
    {
        return toAjax(bizBusinessService.updateBizBusiness(bizBusiness));
    }

    /**
     * 删除流程业务
     */
    @PostMapping("remove")
    public R remove(String ids)
    {
        return toAjax(bizBusinessService.deleteBizBusinessLogic(ids));
    }
}
